package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.CompanyInfoMapper;
import com.suyuan.dao.DepartmentInfoMapper;
import com.suyuan.dao.EmployeeInfoMapper;
import com.suyuan.entity.*;
import com.suyuan.service.EmployeeService;
import com.suyuan.vo.EmployeeVO;
import com.suyuan.web.AuthenticationRealm;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工管理业务类
 */
@Service(value = "employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeInfoMapper employeeMapper;
    @Resource
    private CompanyInfoMapper companyMapper;
    @Resource
    private DepartmentInfoMapper departmentMapper;
    @Resource
    private AuthenticationRealm authenticationRealm;

    public PageInfo list(Integer employeeId, Integer compId, Integer depId, Page page) {
        EmployeeInfoExample example = new EmployeeInfoExample();
        EmployeeInfoExample.Criteria criteria = example.createCriteria();
        if(employeeId != null){
            criteria.andGidEqualTo(employeeId);
        }
        if(compId != null){
            criteria.andCompidEqualTo(compId);
        }
        if(depId != null){
            criteria.andDepidEqualTo(depId);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<EmployeeInfo> employees = employeeMapper.selectByExample(example);
        PageInfo info = new PageInfo(employees);
        if(employees == null || employees.size() <= 0){
            return null;
        }
        List<EmployeeVO> vos = new ArrayList<EmployeeVO>();
        for(EmployeeInfo emp : (List<EmployeeInfo>)info.getList()){
            CompanyInfo company = companyMapper.selectByPrimaryKey(emp.getCompId());
            DepartmentInfo dep = departmentMapper.selectByPrimaryKey(emp.getDepId());
            vos.add(new EmployeeVO(emp,company,dep));
        }
        info.setList(vos);
        return info;
    }

    public int save(EmployeeInfo employee) {
        return employeeMapper.insert(employee);
    }

    public int delete(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 编辑时返回所点击的对象
     * @return
     */
    public EmployeeInfo findById(Integer id) {
        EmployeeInfo employ = employeeMapper.selectByPrimaryKey(id);
        if(employ == null){
            return null;
        }
        //CompanyInfo company = companyMapper.selectByPrimaryKey(employ.getCompId());
        //DepartmentInfo dep = departmentMapper.selectByPrimaryKey(employ.getDepId());
        //return new EmployeeVO(employ,company,dep);
        return employ;
    }

    public EmployeeInfo findByName(String username) {
        EmployeeInfoExample example = new EmployeeInfoExample();
        EmployeeInfoExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isBlank(username)){
            return null;
        }
        criteria.andUsernameEqualTo(username);
        List<EmployeeInfo> emps = employeeMapper.selectByExample(example);
        if(emps.size() > 0){
            return emps.get(0);
        }
        return null;
    }

    public List<CompanyInfo> getCompanys() {
        List<CompanyInfo> companys = companyMapper.selectByExample(new CompanyInfoExample());
        return companys;
    }

    public List<DepartmentInfo> getDepts() {
        List<DepartmentInfo> depts = departmentMapper.selectByExample(new DepartmentInfoExample());
        return depts;
    }

    public int update(EmployeeInfo employee) {
        int code = employeeMapper.updateByPrimaryKey(employee);
        return code;
    }

    /**
     * 根据账户查找用户（登录）
     * @param username
     * @return
     */
    public EmployeeInfo getByUsername(String username) {
        if(username != null){
            EmployeeInfoExample employeeInfoExample = new EmployeeInfoExample();
            EmployeeInfoExample.Criteria criteria = employeeInfoExample.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<EmployeeInfo> employeeInfoList = employeeMapper.selectByExample(employeeInfoExample);
            if(employeeInfoList.size() >0 ){
                return employeeInfoList.get(0);
            }
                return null;
        }
        return null;
    }

    //根据账号获取权限
    public List<Permission> getPermissions(String username) {
        List<Permission> permissionList = employeeMapper.selectPermissionByUsername(username);
        return permissionList;
    }

    public void logout() {
        authenticationRealm.clearCached();
    }


    /**
     * 商城后台登录，检验用户账号密码
     * @param employeeInfo
     * @return
     */
    public EmployeeInfo checkEmployeeInfo(EmployeeInfo employeeInfo) {
        if(employeeInfo == null || StringUtils.isBlank(employeeInfo.getUsername())  || StringUtils.isBlank(employeeInfo.getPassword())){
            return null;
        }
        EmployeeInfoExample employeeInfoExample = new EmployeeInfoExample();
        EmployeeInfoExample.Criteria criteria = employeeInfoExample.createCriteria();
        criteria.andUsernameEqualTo(employeeInfo.getUsername());
        List<EmployeeInfo> employeeInfoList = employeeMapper.selectByExample(employeeInfoExample);
        if(employeeInfoList.size() >0){
           if( employeeInfoList.get(0).getPassword().equals(employeeInfo.getPassword())){
                return employeeInfoList.get(0);
           }
           return  null;
        }
        return null;
    }
}
